//
//  HJNewsDailyCell.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJNewsDailyCell.h"
#import "QMUIButton.h"
#import "HJInformationOperateView.h"
#import "HJImageView.h"
#import "YYText.h"

@interface HJNewsDailyCell()

@property (nonatomic, weak) UIView *pointView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) HJImageView *contentImageView;
@property (nonatomic, weak) UILabel *contentLabel;
@property (nonatomic, weak) QMUIButton *showAllButton;
@property (nonatomic, weak) UILabel *sourceLabel;
@property (nonatomic, weak) HJInformationOperateView *operateView;
@property (nonatomic, weak) UIButton *foldButton;
@property (nonatomic, weak) UIView *tj_contentView;
@property (nonatomic, weak) UIView *lineView;

@end

@implementation HJNewsDailyCell



- (void)settingSubView {
    [super settingSubView];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *lineView = [UIView addToView:self.contentView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    
    UIView *pointView = [UIView addToView:self.contentView];
    pointView.clipsToBounds = YES;
    pointView.addCornerRadius(3);
    pointView.backgroundColor = UIColor.tj_333333Color;
    
    UIView *tj_contentView = [UIView addToView:self.contentView];
    tj_contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    tj_contentView.addCornerRadius(10);
    
    UILabel *timeLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:tj_contentView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    titleLabel.numberOfLines = 0;
    
    UILabel *contentLabel = [UILabel lableWithSuperView:tj_contentView font:UIFontMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    contentLabel.numberOfLines = 0;
    
    HJImageView *contentImageView = [HJImageView addToView:tj_contentView];
    contentImageView.clipsToBounds = YES;
    contentImageView.contentMode = UIViewContentModeScaleAspectFill;
    contentImageView.addCornerRadius(10);
    
    UILabel *sourceLabel = [UILabel lableWithSuperView:tj_contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    
    QMUIButton *foldButton = [QMUIButton buttonWithSuperView:tj_contentView font:UIFontMake(12) color:UIColor.tj_mainColor title:nil];
    [foldButton setImagePosition:QMUIButtonImagePositionRight];
    [foldButton setImage:[UIImage imageNamed:@"message_top"] forState:UIControlStateNormal];
    [foldButton setImage:[UIImage imageNamed:@"message_down"] forState:UIControlStateSelected];
    [foldButton setTitle:@"Conceal" forState:UIControlStateNormal];
    [foldButton setTitle:@"All" forState:UIControlStateSelected];
    [foldButton setSpacingBetweenImageAndTitle:4];
    [foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
    _foldButton = foldButton;
    
    HJInformationOperateView *operateView = [[HJInformationOperateView alloc] initWithTypes:@[
        @(HJInformationOperateLike),
        @(HJInformationOperateShare),
        @(HJInformationOperateViews),
    ]];
    
    TJWeakify(self)
    [operateView setDidClickButtonBlock:^(UIButton * _Nonnull button, HJInformationOperateType type) {
        if ([weakself.delegate respondsToSelector:@selector(informationCellDidClickOperateViewButton:type:button:)]) {
            [weakself.delegate informationCellDidClickOperateViewButton:weakself type:type button:button];
        }
    }];
    
    [tj_contentView addSubview:operateView];
    
    _timeLabel = timeLabel;
    _pointView = pointView;
    _titleLabel = titleLabel;
    _contentImageView = contentImageView;
    _contentLabel = contentLabel;
    _sourceLabel = sourceLabel;
    _operateView = operateView;
    _tj_contentView = tj_contentView;
    _lineView = lineView;
}

- (void)didClickFoldButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickFoldButton:foldButton:)]) {
        [self.delegate informationCellDidClickFoldButton:self foldButton:button];
    }
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    _tj_contentView.frame = self.layout.contentRect;
    _lineView.frame = self.layout.lineRect;
    _pointView.frame = self.layout.pointRect;
    _timeLabel.frame = self.layout.timeRect;
    _titleLabel.frame = self.layout.titleRect;
    _contentLabel.frame = self.layout.textRect;
    _sourceLabel.frame = self.layout.sourceRect;
    _operateView.frame = self.layout.operateRect;
    _contentImageView.frame = self.layout.imageRect;
    
    if (self.layout.isShowAllButton) {
        _foldButton.frame = self.layout.foldButtonRect;
    } else {
        _foldButton.frame = CGRectZero;
    }
}


- (void)setLayout:(HJNewsDailyLayout *)layout {
    _layout = layout;
    
    if (layout.model.images.count) {
        _contentImageView.hidden = NO;
        [_contentImageView tj_setImageWithUrl:layout.model.images.firstObject placeholderImage:[UIImage imageNamed:@"bigLoad"]];
    } else {
        _contentImageView.hidden = YES;
        _contentImageView.image = nil;
    }
    
    if (layout.model.type == 2) {
        _foldButton.hidden = YES;
        return;
    }
    
    
    if (!layout.model.publishTime) {
        layout.model.publishTime = [[NSDate dateWithTimeIntervalSince1970:layout.model.publishTimestamp] stringWithFormat:@"HH:mm"];
    }
    
    _foldButton.hidden = !layout.isShowAllButton;
    _foldButton.selected = layout.isFold;
    
    _contentLabel.attributedText = layout.contentAttributedString;
    _timeLabel.text = layout.model.publishTime;
    _titleLabel.attributedText = layout.titleAttributedString;
    
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    _sourceLabel.text = [NSString stringWithFormat:@"%@", layout.model.source];
    _operateView.likeButton.selected = layout.model.is_like;
    [_operateView.likeButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.like] forState:UIControlStateNormal];
    [_operateView.shareButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.share] forState:UIControlStateNormal];
    [_operateView.viewsButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.views] forState:UIControlStateNormal];
    [_operateView.commentButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.comment] forState:UIControlStateNormal];
    
}



@end
